home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / demos / oversphere.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.8 KB  |  236 lines

  1. /* Copyright (c) Silicon Graphics, Inc. 1996 */
  2.  
  3. /* 
  4.  * oversphere.c
  5.  * 
  6.  * I'd gotten some requests for GLUT to support overlays; that's
  7.  * why overlay support is included in GLUT 3.0.  I didn't get a
  8.  * chance to include many example programs that use overlays though.
  9.  * 
  10.  * Here's a spiffy little program that uses overlays for positioning
  11.  * 3D objects within a simple 3D scene.  It didn't make the GLUT 3.0
  12.  * source distribution (that you can get from
  13.  * http://reality.sgi.com/employees/mjk_asd/glut3/glut3.html ).
  14.  * 
  15.  * Enjoy.
  16.  * 
  17.  * - Mark
  18.  *
  19.  *    Left Mouse Button    - position red spheres
  20.  *    Middle Mouse Button    - position green spheres
  21.  *    Right Mouse Button    - position blue spheres
  22.  *    Escape key        - exit program
  23.  */
  24. /* cc -o oversphere oversphere.c -lglut -lGL -lGLU -lXmu -lXext -lX11 -lm */
  25.  
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <GL/glut.h>
  29.  
  30. #define MAX_SPHERES 50
  31.  
  32. typedef struct {
  33.     GLfloat x, y, z;
  34.     int detail;
  35.     int material;
  36. } SphereInfo;
  37.  
  38. GLfloat lightPos[4] = { 2.0, 4.0, 2.0, 1.0};
  39. GLfloat lightDir[4] = { -2.0, -4.0, -2.0, 1.0};
  40. GLfloat lightAmb[4] = { 0.2, 0.2, 0.2, 1.0};
  41. GLfloat lightDiff[4] = { 0.8, 0.8, 0.8, 1.0};
  42. GLfloat lightSpec[4] = { 0.4, 0.4, 0.4, 1.0};
  43. GLfloat matColor[3][4] = { {0.5, 0.0, 0.0, 1.0},
  44.                {0.0, 0.5, 0.0, 1.0},
  45.                {0.0, 0.0, 0.5, 1.0},
  46.                 };
  47.  
  48. GLdouble modelMatrix[16], projMatrix[16];
  49. GLint viewport[4];
  50. int width, height;
  51. int opaque, transparent;
  52. SphereInfo sphereInfo[MAX_SPHERES];
  53. int spheres = 0;
  54. SphereInfo overlaySphere, oldOverlaySphere;
  55.  
  56. GLvoid  keyboard( GLubyte, GLint, GLint );
  57.  
  58. void
  59. drawSphere(SphereInfo * sphere)
  60. {
  61.     glPushMatrix();
  62.     glTranslatef(sphere->x, sphere->y, sphere->z);
  63.     glMaterialfv(GL_FRONT_AND_BACK,
  64.         GL_AMBIENT_AND_DIFFUSE, matColor[sphere->material]);
  65.     glutSolidSphere(1.0, sphere->detail, sphere->detail);
  66.     glPopMatrix();
  67. }
  68.  
  69. void
  70. display(void)
  71. {
  72.     int i;
  73.  
  74.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  75.     for (i = 0; i < spheres; i++) {
  76.         drawSphere(&sphereInfo[i]);
  77.     }
  78.     glutSwapBuffers();
  79. }
  80.  
  81. void
  82. overlayDisplay(void)
  83. {
  84.     if (glutLayerGet(GLUT_OVERLAY_DAMAGED)) {
  85.         /* If damaged, clear the overlay. */
  86.         glClear(GL_COLOR_BUFFER_BIT);
  87.     } else {
  88.         /* If not damaged, undraw last overlay sphere. */
  89.         glIndexi(transparent);
  90.         drawSphere(&oldOverlaySphere);
  91.     }
  92.     glIndexi(opaque);
  93.     drawSphere(&overlaySphere);
  94.     /* Single buffered window needs flush. */
  95.     glFlush();
  96.     /* Remember last overaly sphere position for undrawing. */
  97.     oldOverlaySphere = overlaySphere;
  98. }
  99.  
  100. void
  101. reshape(int w, int h)
  102. {
  103.     width = w;
  104.     height = h;
  105.     /* Reshape both layers. */
  106.     glutUseLayer(GLUT_OVERLAY);
  107.     glViewport(0, 0, w, h);
  108.     glutUseLayer(GLUT_NORMAL);
  109.     glViewport(0, 0, w, h);
  110.     /* Read back viewport for gluUnProject. */
  111.     glGetIntegerv(GL_VIEWPORT, viewport);
  112. }
  113.  
  114. GLvoid 
  115. keyboard( GLubyte key, GLint x, GLint y )
  116. {
  117.     if (key == 27) { /* escape key */
  118.         exit(0);
  119.     }
  120. }
  121.  
  122. void
  123. mouse(int button, int state, int x, int y)
  124. {
  125.     GLdouble objx, objy, objz;
  126.  
  127.     gluUnProject(x, height - y, 0.95,
  128.         modelMatrix, projMatrix, viewport,
  129.         &objx, &objy, &objz);
  130.     overlaySphere.x = objx;
  131.     overlaySphere.y = objy;
  132.     overlaySphere.z = objz;
  133.     overlaySphere.material = button;
  134.     glutUseLayer(GLUT_OVERLAY);
  135.     glutSetColor(opaque,
  136.         2 * matColor[button][0],  /* Red. */
  137.     2 * matColor[button][1],  /* Green. */
  138.     2 * matColor[button][2]);  /* Blue. */
  139.     if (state == GLUT_UP) {
  140.         glutHideOverlay();
  141.         if (spheres < MAX_SPHERES) {
  142.             sphereInfo[spheres] = overlaySphere;
  143.             sphereInfo[spheres].detail = 25; /* Fine tesselation. */
  144.             spheres++;
  145.         } else {
  146.             printf("oversphere: Out of spheres.\n");
  147.         }
  148.         glutPostRedisplay();
  149.     } else {
  150.         overlaySphere.detail = 10;  /* Coarse tesselation. */
  151.         glutShowOverlay();
  152.         glutPostOverlayRedisplay();
  153.     }
  154. }
  155.  
  156. void
  157. motion(int x, int y)
  158. {
  159.     GLdouble objx, objy, objz;
  160.  
  161.     gluUnProject(x, height - y, 0.95,
  162.         modelMatrix, projMatrix, viewport,
  163.         &objx, &objy, &objz);
  164.     overlaySphere.x = objx;
  165.     overlaySphere.y = objy;
  166.     overlaySphere.z = objz;
  167.     glutPostOverlayRedisplay();
  168. }
  169.  
  170. void
  171. setupMatrices(void)
  172. {
  173.     glMatrixMode(GL_PROJECTION);
  174.     gluPerspective( /* degrees field of view */ 50.0,
  175.         /* aspect ratio */ 1.0, /* Z near */ 1.0, /* Z far */ 10.0);
  176.     glMatrixMode(GL_MODELVIEW);
  177.     gluLookAt( 0.0, 0.0, 5.0,      /* eye is at (0,0,5) */
  178.         0.0, 0.0, 0.0,      /* center is at (0,0,0) */
  179.         0.0, 1.0, 0.);      /* up is in positive Y direction */
  180. }
  181.  
  182. int
  183. main(int argc, char **argv)
  184. {
  185.     glutInitWindowSize(350, 350);
  186.     glutInit(&argc, argv);
  187.  
  188.     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
  189.     glutCreateWindow("Overlay Sphere Positioning Demo");
  190.     glutDisplayFunc(display);
  191.     glutReshapeFunc(reshape);
  192.     glutMouseFunc(mouse);
  193.     glutMotionFunc(motion);
  194.     glutKeyboardFunc( keyboard );
  195.  
  196.     glEnable(GL_DEPTH_TEST);
  197.     glEnable(GL_CULL_FACE);  /* Solid spheres benefit greatly
  198.                                   from back face culling. */
  199.     setupMatrices();
  200.     /* Read back matrices for use by gluUnProject. */
  201.     glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
  202.     glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
  203.  
  204.     /* Set up lighting. */
  205.     glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
  206.     glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmb);
  207.     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiff);
  208.     glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpec);
  209.     glEnable(GL_LIGHT0);
  210.     glEnable(GL_LIGHTING);
  211.  
  212.     glutInitDisplayMode(GLUT_INDEX | GLUT_SINGLE);
  213.     if (glutLayerGet(GLUT_OVERLAY_POSSIBLE) == 0) {
  214.         printf("oversphere: no overlays supported; aborting.\n");
  215.         exit(1);
  216.     }
  217.     glutEstablishOverlay();
  218.     glutHideOverlay();
  219.     glutOverlayDisplayFunc(overlayDisplay);
  220.  
  221.     /* Find transparent and opaque index. */
  222.     transparent = glutLayerGet(GLUT_TRANSPARENT_INDEX);
  223.     opaque = (transparent + 1)
  224.         % glutGet(GLUT_WINDOW_COLORMAP_SIZE);
  225.  
  226.     /* Draw overlay sphere as an outline. */
  227.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  228.     /* Make sure overlay clears to transparent. */
  229.     glClearIndex(transparent);
  230.     /* Set up overlay matrices same as normal plane. */
  231.     setupMatrices();
  232.  
  233.     glutMainLoop();
  234.     return 0;
  235. }
  236.